home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1146 / 1146.xpi / chrome / screengrab.jar / content / Action.js next >
Text File  |  2009-03-09  |  4KB  |  105 lines

  1.  
  2. screengrab.Action = function() {}
  3. screengrab.Action.prototype = {
  4.     doAction: function(canvas) {}
  5. }
  6.  
  7. screengrab.SaveAction = function() {}
  8. screengrab.SaveAction.prototype = {
  9.     doAction: function(canvas) {
  10.         var msg = document.getElementById("screengrab-strings").getString("SaveAsMessage");
  11.         var picker = new screengrab.ImageFilePicker(screengrab.prefs.defaultFileName() + "." + screengrab.prefs.format(), msg, screengrab.prefs.formatMimeType());
  12.         var file = picker.getFile();
  13.         var dataUrl = canvas.toDataURL(file.mimeType, screengrab.prefs.formatQuality(file.mimeType));
  14.         file.saveDataUrl(dataUrl, true);
  15.     }
  16. }
  17.  
  18. screengrab.CopyAction = function() {}
  19. screengrab.CopyAction.prototype = {
  20.     doAction: function(canvas) {
  21.         var dataUrl = canvas.toDataURL("image/png", "");
  22.         screengrab.Clipboard.putImgDataUrl(dataUrl, null);
  23.     }
  24. }
  25.  
  26. screengrab.UploadScrnshotsAction = function(shotData) {
  27.     this.shotData = shotData;
  28. }
  29. screengrab.UploadScrnshotsAction.prototype = {
  30.     doAction: function(canvas) {
  31.         try {
  32.             var me = this;
  33. //            this.shotData = new sg.ScrnShots.ShotData();
  34. //            this.shotData.description = "A description";
  35. //            this.shotData.tags = "tags what";
  36. //            this.shotData.sourceUri = "www.screengrab.org";
  37. //            this.shotData.uploadFilename = "screengrab.png";
  38. //            this.shotData.sourceUri = "www.screengrab.org";
  39.             this.shotData.file = screengrab.File.newTempFile("temp", "image/png");
  40.             this.shotData.file.saveDataUrlWithCallbackWhenDone(canvas.toDataURL("image/png", ""), function() {
  41.                 try {
  42.                     new sg.ScrnShots("bjorkmann", "raddest1").upload(me.shotData); 
  43.                     //me.doUpload(shotData);
  44.                 } catch (error) {
  45.                     sg.error(error);
  46.                 }
  47.             });
  48.         } catch (error) {
  49.             sg.error(error);
  50.         }
  51.     },
  52.     
  53.     doUpload: function(sgFile) {
  54.         var boundStr = '---------------------------265001916915724';
  55.         var boundary = '--' + boundStr;
  56.         var req = new XMLHttpRequest();
  57.         var bodyStart = 
  58.             boundary + "\r\n" +
  59.             'Content-Disposition: form-data; name="screenshot[description]"' + "\r\n" +
  60.             "\r\n" +
  61.             this.description + "\r\n" +
  62.             boundary + "\r\n" +
  63.             'Content-Disposition: form-data; name="screenshot[tag_list]"' + "\r\n" +
  64.             "\r\n" +
  65.             this.tags + "\r\n" +
  66.             boundary + "\r\n" +
  67.             'Content-Disposition: form-data; name="screenshot[source_url]"' + "\r\n" +
  68.             "\r\n" +
  69.             this.uri + "\r\n" +
  70.             boundary + "\r\n" +
  71.             'Content-Disposition: form-data; name="screenshot[uploaded_data]"; filename="' + this.filename + '"' + "\r\n" +
  72.             'Content-Type: image/png' + "\r\n" +
  73.             "\r\n";
  74.         var bodyStartStream = new sg.File.StringInputStream(bodyStart, bodyStart.length);
  75.         var fileStream = sgFile.getBufferedStream();
  76.         var bodyEnd = 
  77.             "\r\n" +
  78.             boundary + "--";
  79.         var bodyEndStream = new sg.File.StringInputStream(bodyEnd, bodyEnd.length);
  80.         
  81.         var multiplexed = new sg.File.MultiplexInputStream();
  82.         multiplexed.appendStream(bodyStartStream);
  83.         multiplexed.appendStream(fileStream);
  84.         multiplexed.appendStream(bodyEndStream);
  85.         var length = bodyStartStream.available() + fileStream.available() + bodyEndStream.available(); 
  86.         req.open("POST", "http://www.scrnshots.com/screenshots.xml", true);
  87.         req.setRequestHeader("Authorization", "Basic " + sg.Base64.encode("bjorkmann:raddest1"));
  88.         req.setRequestHeader("Content-type", "multipart/form-data; boundary=" + boundStr);
  89.         req.setRequestHeader("Content-Length", length);
  90.         req.setRequestHeader("Connection", "close");
  91.         req.overrideMimeType('text/xml');
  92.         req.onreadystatechange = function() {
  93.             if (req.readyState == 4) {
  94.                 var links = req.responseXML.getElementsByTagName('link');
  95.                 if (links.length != 1) {
  96.                     alert("Something went wrong...");
  97.                 } else {
  98.                     gBrowser.selectedTab = gBrowser.addTab(links[0].childNodes[0].nodeValue);
  99.                 }
  100.                 sgFile.remove();
  101.             }
  102.         }
  103.         req.send(multiplexed);
  104.     }
  105. }